package com.yixiang.np.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yixiang.np.dao.ResourceDao;
import com.yixiang.np.model.SessionInfo;
import com.yixiang.np.model.Tree;
import com.yixiang.np.model.po.Resource;
import com.yixiang.np.service.ResourceServiceI;

@Service
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class ResourceServiceImpl implements ResourceServiceI {

	@Autowired
	private ResourceDao resourceDao;

	@Override
	public List<Resource> treeGrid() {
		List<Resource> l = resourceDao.findAllData();
		return l;
	}

	@Override
	public void add(Resource resource) {
		resource.setState(resource.getCstate());
		resource.setCreatedatetime(new Date());
		resourceDao.save(resource);
	}

	@Override
	public void delete(Long id) {
		resourceDao.deleteByPid(id);
		resourceDao.delete(id);
	}

	@Override
	public void edit(Resource resource) {
		resource.setState(resource.getCstate());
		resourceDao.update(resource);
	}

	@Override
	public Resource get(Long id) {
		return resourceDao.get(id);
	}

	@Override
	public List<Tree> tree(SessionInfo sessionInfo) {
		List<Resource> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("resourcetype", 0);// 菜单类型的资源

		if (sessionInfo != null) {
			if("admin".equals(sessionInfo.getLoginname())){
				l = resourceDao
						.findAll(params);
			}else{
				params.put("userId", Long.valueOf(sessionInfo.getId()));// 自查自己有权限的资源
				l = resourceDao
					.findByRoles(params);
			}
		} else {
			return null;
		}

		if ((l != null) && (l.size() > 0)) {
			for (Resource r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getPid() != null) {
					tree.setPid(r.getPid().toString());
				}else{
					tree.setState("closed");
				}
				tree.setText(r.getName());
				tree.setIconCls(r.getIcon());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.getUrl());
				tree.setAttributes(attr);
				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
	public List<Tree> allTree(boolean flag) {
		List<Resource> l = null;
		List<Tree> lt = new ArrayList<Tree>();
		if(flag){
			l = resourceDao.findAllData();
		}else{
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("resourcetype", 0);// 菜单类型的资源
			l = resourceDao.findAll(params);
		}
		if ((l != null) && (l.size() > 0)) {
			for (Resource r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getPid() != null) {
					tree.setPid(r.getPid().toString());
				}
				tree.setText(r.getName());
				tree.setIconCls(r.getIcon());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.getUrl());
				tree.setAttributes(attr);
				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
	public List<String> resourceAllList() {
		List<String> resourceList = new ArrayList<String>();
		List<Resource> l = resourceDao.findAllData();
		for (int i = 0; i < l.size(); i++) {
			resourceList.add(l.get(i).getUrl());
		}
		return resourceList;
	}

	@Override
	public String resourceString(Long pid,Long userid) {
	    String resource = "<dl class=\"leftmenu\">";
	    Map<String, Object> attr = new HashMap<String, Object>();
		attr.put("pid", pid);
		attr.put("userid", userid);
		List<Resource> l = resourceDao.findByPid(attr);
		for (Resource r : l) {
			resource += "<dd>";
			resource += "<div class=\"title\">";
			resource += "<span></span>" + r.getName();
//			resource += "<span><a href=\"javascript:void(0)\" class=\"easyui-linkbutton\" "
//					+ "data-options=\"iconCls:'icon-save',plain:true\"/></span>" + r.getName();
			resource += "</div>";
			resource += "<ul class=\"menuson\">";
			attr.put("pid", r.getId());
			attr.put("userid", userid);
			List<Resource> ll = resourceDao.findByPid(attr);
			for (Resource rr : ll) {
				String param = "{url : '"+rr.getUrl()+"',title : '"+rr.getName()+"',iconCls : '"+rr.getIconCls()+"'}";
				resource += "<li><cite></cite><a href=\"#\" onclick=\"addTab("+param+")\">"+rr.getName()+"</a><i></i></li>";
			}
			resource += "</ul>";
			resource += "</dd>";
		}
		resource += "</dl>";
		return resource;
	}
	@Override
	public String resourceFirString(Long pid,Long userid,String path) {
		String resource = "";
		Map<String, Object> attr = new HashMap<String, Object>();
		attr.put("pid", pid);
		attr.put("userid", userid);
		List<Resource> l = resourceDao.findByPid(attr);
		for (Resource r : l) {
		    if(r.getResourcetype().equals(0)){
                resource += "<li class=\"nav1\">"+r.getName()+"<img class=\"triangle\" src=\""+path+"/image/triangle.gif\"></li>";
            }
		}
		return resource;
	}
	@Override
	public String resourceSecString(Long pid,Long userid,String path) {
		String resource = "";
		Map<String, Object> attr = new HashMap<String, Object>();
		attr.put("pid", pid);
		attr.put("userid", userid);
		List<Resource> l = resourceDao.findByPid(attr);
		for (Resource r : l) {
            if(r.getResourcetype().equals(0)){
                resource += "<ul class=\"navSec\" >";
                attr.put("pid", r.getId());
                attr.put("userid", userid);
                List<Resource> ll = resourceDao.findByPid(attr);
                for (Resource rr : ll) {
                    if(rr.getResourcetype().equals(0)){
                        resource += "<li><a href=\""+path+"/resource/head/"+r.getId()+"/"+rr.getId()+"\">"+rr.getName()+"</a></li>";
                    }
                }
                resource += "</ul>";
            }
		}
		return resource;
	}

	@Override
	public List<Resource> userResource(Long userid) {
		return resourceDao.userResource(userid);
	}
}
